﻿/******************************************************************************
  * Copyright  2023 - LiuKuan
  * All right reserved. See COPYRIGHT for detailed Information.
  *
  * @file       LineTempController.h
  * @brief
  * @author     <807874484@qq.com>
  * @date       2024/07/17
  * @history
  *****************************************************************************/

#ifndef LINETEMPCONTROLLER_H
#define LINETEMPCONTROLLER_H

#include <QObject>
#include <QFileInfo>
#include <QVector>
#include <QTimer>

#include "xlsx/xlsxdocument.h"
#include "Stdafx.h"

#include "LineTempFactGroup.h"
#include "GroundedFactGroup.h"
#include "global/LinkManager.h"


class TowerDevicesController : public QObject
{
    Q_OBJECT
    ///是否处于连接状态
    Q_PROPERTY(bool connected     READ connected    WRITE setConnected NOTIFY connectedChanged)

//    Q_PROPERTY(bool debuggerMode  READ debuggerMode  NOTIFY setDebuggerMode)
    ///主机地址
    Q_PROPERTY_AUTO(int  ,serverAddress)
public:
    enum Device_Type {
            LineTemp = 0x0000,
            Grounded = 0x0001
        };
    Q_ENUM(Device_Type)

    explicit TowerDevicesController(QObject *parent = nullptr);
    ~TowerDevicesController();
    ///创建基础配置文件
    Q_INVOKABLE bool createSettingsFile(Device_Type type,
                                        int address_num,
                                        const QString &id,
                                        const QString & factor,
                                        const QString &desc);
    ///配置文件是否存在
    Q_INVOKABLE bool isfileExist(Device_Type type);
    ///打开配置文件(QML)
    Q_INVOKABLE bool openSettingsFile(Device_Type type);
    ///连接设备
    Q_INVOKABLE bool connectLink(const QString & port_name,
                                 int baud,int data_bits,
                                 int stop_bits,int parity);


    Q_INVOKABLE bool connectDebuggerLink(const QString & port_name,
                                         int baud,int data_bits,
                                         int stop_bits,int parity);



    bool debuggerMode(bool isHex);
    ///
    /// \brief setDebuggerMode
    /// \param isHex
    ///
    void setDebuggerMode(bool isHex);
    ///
    /// \brief disConnectDebuggerLink 断开Debugger的连接
    ///
    Q_INVOKABLE void disConnectDebuggerLink();
    ///
    /// \brief writeBytesToDebugger 向Debugger写入命令
    /// \param data
    ///
    Q_INVOKABLE void writeBytesToDebugger(const QString &data);
    ///主动断开设备连接
    Q_INVOKABLE void disConnectedLink();
    ///写入设备参数
    Q_INVOKABLE bool writeSettingsToDevice(Device_Type type);
    ///查询设备参数
    Q_INVOKABLE void readSettingsFromDevice(Device_Type type);
    ///QML USED
    Q_INVOKABLE LineTempFactGroup *getLineTempGroupByIndex(int index);
    Q_INVOKABLE GroundedFactGroup *getGroundedGroupByIndex(int index);
    ///判断配置文件是否
    Q_INVOKABLE bool isSettingsFileVaild(Device_Type type);
    ///是否连接
    bool connected() const;
    ///导出XLSX文件
    Q_INVOKABLE bool exportXlsxFile(Device_Type type,const QString & file_name);
signals:
    void operationResult(int msglevel ,const QString & result_str);
    void connectedChanged(bool state);
    ///读取配置文件完成
    void readSettingsFinished();
    ///写入配置文件完成
    void writeSettingsFinished();
    ///
    /// \brief readDebuggerMsg 接收到Debugger的数据
    /// \param str
    ///
    void readDebuggerMsg(const QString & str);
    void writeDebggerMsg(const QString & str);
private slots:
    void handleLineTempModbusProto(int serverAddress, int startAddress,const QModbusDataUnit &unit);
private:
    QString getFormatDataString(const QString &custom);
    void initAllGroups();
    void clearGroupsData(Device_Type type);
    void setConnected(bool state);
    void byteArrayToIntVector(QVector<quint16>&vec,QByteArray &msg);
    void insertSettingsHeader(QXlsx::Document &xlsx);
    void insertExportFileHeader(Device_Type type , QXlsx::Document & xlsx);
////-------------------------------LineTemp---------------------------------///
    void setDescribeInfo(int startAddress, const QString& dest);
    void setIdAndFactor(int startAddress, const QString &id,const double factory);

    void readDescribeInfo(int startAddress);
    void readIdAndFactor(int startAddress);
    void readValueData(int startAddress);
private:
    bool  isHexDisplay_ = false;
    int readTaskRemain  {0};
    int writeTaskRemain {0};
    QFileInfo line_temp_info_;
    QFileInfo grounded_info_;
    ModbusRtuLink * link_{nullptr};
    SerialLink *    debuggerLink_{nullptr};
    SerialDebugger *debugger_{nullptr};
    bool connected_{false};
    //查询结果//
    QVector<LineTempFactGroup*> lineTempGroups_{};
    //查询结果//
    QVector<GroundedFactGroup*> groundedGroups_{};
};

#endif // LINETEMPCONTROLLER_H
